//
//  HEIMCircleChatController.m
//  HealthElderly
//
//  Created by 吴欧 on 2021/2/26.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEIMCircleChatController.h"
#import "HEIMTextCell.h"
#import "HEIMInfoEntity.h"
#import "HEIMForward.h"
#import "HEActivityHeader.h"
#import "HEIMImageViewCell.h"
#import "HEIMAudioCell.h"
#import "HEMediaManager.h"
#import "HEChatManager.h"
#import "HEIMConsulReportCell.h"
#import "HEConsulReportViewController.h"
#import "HETeamModel.h"
#import "HEDocImMsgModel.h"
#import "HEIMProductCell.h"
#import "HEGoodsDetailsViewController.h"
#import "HEItemBtnView.h"
#import "HEDoctorMainPageViewController.h"
#import "HEIMCommentCell.h"
#import "HEATPersonListViewController.h"
#import "HEChatBar.h"
#import "HECircleChatManager.h"
#import "HERoomUserTopView.h"
#import "HEIMRoomInAndoutCell.h"
#import "LEEAlert.h"
#import "HECircleMoreViewController.h"
#import "HEIMInfoPageCell.h"
#import "HEEduVideoDetailsController.h"
#import "HEIMInfoVideoPageCell.h"

@interface HEIMCircleChatController ()<UITableViewDelegate, UITableViewDataSource, HEIMMessageCellDelegate, HEChatBarDelegate, HECircleChatManagerDelegate>

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray *sections;
@property (nonatomic, strong) NSMutableArray <HEIMMessageModel *> *dataSource;
@property (nonatomic, strong) NSMutableArray <HEIMMessageModel *> *cacheSendMsg;

@property (nonatomic, strong) HEIMForward *imDataForward;
@property(nonatomic, assign) BOOL fristInto;

@property (nonatomic, strong) UIScrollView *manContent;

@property (nonatomic, strong) HETeamModel *teamModel;

@property (nonatomic, strong) NSString *userIdCrad;
@property (nonatomic, strong) NSMutableArray <HEIMKitInfo *> *teamUsers;

@property (nonatomic, strong) HERoomUserTopView *userTopView;

@property (nonatomic, strong) HEChatBar *chatBar;

/*!
 * 发送消息时，会置YES
 * 输入框高度变更，比如输入文字换行、切换到 More、Face 页面、键盘弹出、键盘收缩
 */
@property (nonatomic, assign) BOOL allowScrollToBottom;

@property (nonatomic, strong) UIImage *logoImage;

@property (nonatomic, strong) HEIMKitInfo *receiveInfo;
@end

@implementation HEIMCircleChatController

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[IQKeyboardManager sharedManager] setEnable:YES];
     [[HEMediaManager sharedManager] stopPlay];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[IQKeyboardManager sharedManager] isEnabled];
    [[IQKeyboardManager sharedManager] setEnable:NO];
    
    [self.chatBar hidekeyBoard];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.chatBar close];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.chatBar open];
}

- (void)dealloc {
    DLog(@"dealloc - room")
     [[HECircleChatManager sharedManager] removeDelegate:self];
}

- (void)initSession {
    HEIMSession *session = [HEIMSession session:_roomId type: HEIMSessionTypeChatroom];
    [[HEIMConversationManager sharedManager] registeSession: session];
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [[HECircleChatManager sharedManager] addDelegate:self];
    _receiveInfo = [[HEIMKit sharedKit] infoByUser: _roomId];
    
    [self initSession];
    
    // 自动加入浮窗
    JPSEInstance.isAutoJoinSuspension = YES;
    JPSEInstance.popNewSuspensionView = [JPSuspensionView suspensionViewWithViewController: self];

    [self initUI];
    [self initCell];

    [self initInputView];

    // 加入聊天室
    [self joinRoom];
}

- (void)initUI {
    self.zx_navBar.backgroundColor = CNavBgColor;
    self.zx_backBtnImageName = @"back_icon";
    self.zx_navLineView.hidden = YES;
    self.zx_navTintColor = KWhiteColor;
    self.zx_navStatusBarStyle = ZXNavStatusBarStyleLight;
    [self.zx_navLeftBtn setTitle:[NSString stringWithFormat:@"%@(0)", _receiveInfo.showName] forState:UIControlStateNormal];

    [self.view addSubview:self.tableView];
    [self.view addSubview:self.userTopView];
    
    KWeakSelf
    [self zx_setRightBtnWithText:@"退出" clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        [weakSelf popLogoutAlter];
    }];
 
    [self zx_setSubRightBtnWithText:@"公告" clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        HECircleMoreViewController *vc = [HECircleMoreViewController new];
        vc.index = 1;
        vc.roomId = weakSelf.roomId;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
}

- (void)popLogoutAlter {
    KWeakSelf
    UIColor *blueColor = [UIColor colorWithRed:90/255.0f green:154/255.0f blue:239/255.0f alpha:1.0f];
    [LEEAlert alert].config
    .LeeAddTitle(^(UILabel *label) {
        label.text = @"确认退出?";
        label.textColor = [UIColor whiteColor];
    })
    .LeeAddContent(^(UILabel *label) {
        label.text = @"退出聊天室后，聊天室消息记录将会清空。您确定要退出吗？";
        label.font = DYBlodFont(16);
        label.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.75f];
    })
    .LeeAddAction(^(LEEAction *action) {
        action.type = LEEActionTypeCancel;
        action.title = @"取消";
        action.titleColor = blueColor;
        action.backgroundColor = [UIColor whiteColor];
        action.clickBlock = ^{
            // 取消点击事件Block
        };
    })
    .LeeAddAction(^(LEEAction *action) {
        action.type = LEEActionTypeDefault;
        action.title = @"确定";
        action.titleColor = blueColor;
        action.backgroundColor = [UIColor whiteColor];
        action.clickBlock = ^{
            // 确定点击事件Block
            JPSEInstance.suspensionView = nil;
            [[HECircleChatManager sharedManager] leaveRoom];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        };
    })
    .LeeHeaderColor(blueColor)
    #ifdef __IPHONE_13_0
    .LeeUserInterfaceStyle(UIUserInterfaceStyleLight)
    #endif
    .LeeShow();
}
 
- (void)initInputView {
    if (!_isExprie) {
        [self.view addSubview:self.chatBar];
        [self.chatBar open];

        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(ZXNavBarHeight + 75);
            make.left.and.width.equalTo(self.view);
            make.bottom.equalTo(self.chatBar.mas_top);
        }];
        [self.chatBar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.equalTo(self.view);
            make.height.mas_greaterThanOrEqualTo(@(kLCCKChatBarMinHeight));
            make.bottom.equalTo(self.view);
        }];

    }else {
        self.tableView.frame = CGRectMake(0, ZXNavBarHeight - 75, KScreenWidth, KScreenHeight - ZXNavBarHeight - KSafeTabbarHeight - 75);
    }
}

- (void)initCell {
    
    _sections = @[].mutableCopy;
    
    NSMutableArray *classArr = [NSMutableArray array];
    
    //
    GDFiterCellModel *textModel = [GDFiterCellModel new];
    textModel.cellKlass = HEIMTextCell.class;
    
    GDFiterCellModel *imageModel = [GDFiterCellModel new];
    imageModel.cellKlass = HEIMImageViewCell.class;
    
    GDFiterCellModel *audioModel = [GDFiterCellModel new];
    audioModel.cellKlass = HEIMAudioCell.class;
    
    GDFiterCellModel *reportCellModel = [GDFiterCellModel new];
    reportCellModel.cellKlass = HEIMConsulReportCell.class;
    
    GDFiterCellModel *productModel = [GDFiterCellModel new];
    productModel.cellKlass = HEIMProductCell.class;
    
    GDFiterCellModel *commentModel = [GDFiterCellModel new];
    commentModel.cellKlass = HEIMCommentCell.class;
    
    GDFiterCellModel *roomInAndoutModel = [GDFiterCellModel new];
    roomInAndoutModel.cellKlass = HEIMRoomInAndoutCell.class;
    
    GDFiterCellModel *infoPageModel = [GDFiterCellModel new];
    infoPageModel.cellKlass = HEIMInfoPageCell.class;
    
    GDFiterCellModel *infoVideoPageModel = [GDFiterCellModel new];
    infoVideoPageModel.cellKlass = HEIMInfoVideoPageCell.class;
    
    
     
    // 添加的时候注意顺序，暂未对顺序做处理
    classArr = @[textModel, imageModel, audioModel, reportCellModel, productModel, commentModel, roomInAndoutModel, infoPageModel, infoVideoPageModel].mutableCopy;
    
    for (GDFiterCellModel *model in classArr) {
        NSString *cellIdOwner = [NSString stringWithFormat:@"%@_%@_%@",NSStringFromClass(model.cellKlass), HECellIdentifierOwnerSelf, HECellIdentifierGroup];
        NSString *cellIdOwnerOther = [NSString stringWithFormat:@"%@_%@_%@",NSStringFromClass(model.cellKlass), HECellIdentifierOwnerOther, HECellIdentifierGroup];

        [self.tableView registerClass:model.cellKlass forCellReuseIdentifier: cellIdOwner];
        [self.tableView registerClass:model.cellKlass forCellReuseIdentifier: cellIdOwnerOther];
    }
    
    NSString *cellIdSystem = [NSString stringWithFormat:@"%@_%@_%@",NSStringFromClass(roomInAndoutModel.cellKlass), HECellIdentifierSystem, HECellIdentifierGroup];
    [self.tableView registerClass:roomInAndoutModel.cellKlass forCellReuseIdentifier: cellIdSystem];
}

#pragma mark - data opratione
- (void)joinRoom {
    UserInfo *user = [UserManager sharedUserManager].curUserInfo;
    HEChatRoomModel *model = [HEChatRoomModel new];
    model.roomId = _roomId.integerValue;
    model.nickname = user.nickname;
    model.userId = user.id;
    model.avatar = user.avatar;
    
    KWeakSelf
    [[HECircleChatManager sharedManager] joinRoom:model callback:^(BOOL success) {
        for (HEIMMessageModel *message in weakSelf.cacheSendMsg) {
            [[HECircleChatManager sharedManager] sendMessage:message callback:^(BOOL success) {
                [weakSelf.cacheSendMsg removeObject:message];
                DLog(@"缓存消息数 ..  %ld", weakSelf.cacheSendMsg.count)
            }];
        }
    }];
}

- (void)sendMessageWithVC:(HEIMMessageModel *)message {
    [self.dataSource addObject:message];
    [self.cacheSendMsg addObject:message];
    
    KWeakSelf
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf.tableView reloadData];
        [weakSelf scrollToBottom];
    });
    if ([HECircleChatManager sharedManager].isInRoom) {
        [[HECircleChatManager sharedManager] sendMessage:message callback:^(BOOL success) {
            [weakSelf.cacheSendMsg removeObject:message];
            DLog(@"缓存消息数 ..  %ld", weakSelf.cacheSendMsg.count)
        }];
    }
}

#pragma mark -

- (void)scrollToBottom
{
    [self.tableView layoutIfNeeded];
    CGFloat yOffset = 0; //设置要滚动的位置 0最顶部 CGFLOAT_MAX最底部
    if (self.tableView.contentSize.height > self.tableView.bounds.size.height) {
        yOffset = self.tableView.contentSize.height - self.tableView.bounds.size.height;
    }
    [self.tableView setContentOffset:CGPointMake(0, yOffset) animated: !self.fristInto];
    
    self.fristInto = NO;
}

#pragma mark - UITableViewDataSource
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    HEIMMessageModel *imModel = self.dataSource[indexPath.row];

    HEIMMessageCell *cell = [_tableView dequeueReusableCellWithIdentifier: imModel.cellIdentifier];
    cell.delegate = self;
    [cell configCellWithModel: imModel];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HEIMMessageModel *imModel = self.dataSource[indexPath.row];

 
  return [tableView fd_heightForCellWithIdentifier: imModel.cellIdentifier cacheByIndexPath:indexPath configuration:^(MCTableBaseCell *cell) {
      [cell configCellWithModel: imModel];

   }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.chatBar endInputing];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.chatBar endInputing];
}

#pragma mark - HEChatBarDelegate
- (void)chatBarFrameDidChange:(HEChatBar *)chatBar shouldScrollToBottom:(BOOL)shouldScrollToBottom {
 
    [UIView animateWithDuration:LCCKAnimateDuration animations:^{
        [self.tableView.superview layoutIfNeeded];
        self.allowScrollToBottom = shouldScrollToBottom;
        [self scrollToBottomAnimated:NO];
        
//        [self scrollToBottom];

    } completion:nil];
}

- (void)scrollToBottomAnimated:(BOOL)animated {
    if (!self.allowScrollToBottom) {
        return;
    }
    NSInteger rows = [self.tableView numberOfRowsInSection:0];
    if (rows > 0) {
        dispatch_block_t scrollBottomBlock = ^ {
            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:rows - 1 inSection:0]
                                  atScrollPosition:UITableViewScrollPositionBottom
                                          animated:animated];
        };
        if (animated) {
            //when use `estimatedRowHeight` and `scrollToRowAtIndexPath` at the same time, there are some issue.
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                scrollBottomBlock();
            });
        } else {
            scrollBottomBlock();
        }
    }
}

- (void)newScrollToBottomAnimated:(BOOL)animated {
    if (!self.allowScrollToBottom) {
        return;
    }
    NSInteger rows = [self.tableView numberOfRowsInSection:0];
    if (rows > 0) {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:rows - 1 inSection:0]
                              atScrollPosition:UITableViewScrollPositionBottom
                                      animated:animated];
    }
}

#pragma mark - HEChatBarDelegate
- (void)sendMessage:(HEIMMessageModel *)message {
    
    [self.dataSource addObject:message];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
        [self scrollToBottom];
    });
    
    [[HECircleChatManager sharedManager] sendMessage:message callback:^(BOOL success) {
    }];
}

#pragma mark - HECircleChatManagerDelegate
- (void)sendMessageCallback:(HEIMMessage *)message {
    [self.tableView reloadData];
}

- (void)onRecvMessages:(HEIMMessageModel *)message {
    UserInfo *user = [UserManager sharedUserManager].curUserInfo;
    if ([_roomId isEqualToString: message.message.to] &&
        ![message.message.from isEqualToString: user.id]) {
        
        [self.dataSource addObject:message];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            [self scrollToBottom];
        });
    }
}

- (void)teamAllUsers:(NSArray<HEIMKitInfo *> *)teams {
    
    [[HEIMKit sharedKit] registeUserInfos: teams];
    _chatBar.teamUserList = self.userTopView.showList;
    [self.zx_navLeftBtn setTitle:[NSString stringWithFormat:@"%@(%ld)", _receiveInfo.showName, self.userTopView.showList.count] forState:UIControlStateNormal];

    [self.userTopView refreshData: teams];
    [HECircleChatManager sharedManager].currentUserList = self.userTopView.showList;
}

- (void)teamUserChangeInfo:(HEChatRoomModel *)user {
    
    if (_roomId.integerValue != user.roomId) {
        return;
    }
    
    HEIMMessage *inOrOutMessage = [HEIMMessage new];
    inOrOutMessage.type = HEIMMessageTypeSystemRoomInAndOutNotice;
    inOrOutMessage.modelType = HEIMUserModelTypeRoom;
    
    BOOL isExsit = NO;
    for (int i = 0; i < self.userTopView.showList.count; i++) {
        HEIMKitInfo *info = self.userTopView.showList[i];
        if ([info.infoId isEqualToString:user.userId]) {
            isExsit = YES;

            if (user.type == HETeamOperationTypeLeave) {
                inOrOutMessage.text = [NSString stringWithFormat:@"%@离开了聊天室", info.showName];
                [self.userTopView.showList removeObjectAtIndex:i];
                [[HEIMKit sharedKit] removeUserInfoById: user.userId];
                break;
            }else {
                break;
            }
        }
    }
    
    if (user.type == HETeamOperationTypeJoin) {
        
        inOrOutMessage.text = [NSString stringWithFormat:@"%@加入了聊天室",user.nickname];

        if (!isExsit) {
            HEIMKitInfo *newInfo = [HEIMKitInfo new];
            newInfo.infoId = user.userId;
            newInfo.avatarUrlString = user.avatar;
            newInfo.showName = user.nickname;
            newInfo.avatarImage = IMAGE_NAMED(@"defualt_icon");
            
            [self.userTopView.showList addObject:newInfo];
            [[HEIMKit sharedKit] registeUserInfo: newInfo];
        }
    }
    
    [self.zx_navLeftBtn setTitle:[NSString stringWithFormat:@"%@(%ld)", _receiveInfo.showName, self.userTopView.showList.count] forState:UIControlStateNormal];

    // 提示离开的人员
    HEIMMessageModel *messageModel = [[HEIMMessageModel alloc] initWithMessage: inOrOutMessage];
    [self.dataSource addObject: messageModel];

    _chatBar.teamUserList = self.userTopView.showList; // @人数据
    [self.userTopView.collectionView reloadData];      // 顶部人员数据
    [HECircleChatManager sharedManager].currentUserList = self.userTopView.showList;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
        [self scrollToBottom];
    });
}

#pragma mark - HESocketManagerDelegate
- (void)loginSuccessHandler {
    DLog(@"重新登录成功")
     
    [self joinRoom];
}

#pragma mark - HEIMMessageCellDelegate
- (void)onTapMessage:(HEIMMessage *)message {
    if (message.type == HEIMMessageTypeAudio) {
        [[HEMediaManager sharedManager] playByMessage: message];
    }else if (message.type == HEIMMessageTypeInfoPage) {
        HEIMInfoPageObject *infoPage = (HEIMInfoPageObject *)message.messageObject;
        NSURL * file = [NSURL URLWithString: [NSString stringWithFormat:@"%@/details/#/text?id=%ld", KServerUrl, infoPage.id]];
 
        HEMissonListModel *missonModel = [HEMissonListModel new];
        missonModel.id = infoPage.id;
        missonModel.audio = infoPage.voiceUrl;
        missonModel.info_title = infoPage.title;
        missonModel.img_url = infoPage.iconUrlStr;
        missonModel.information_brief = infoPage.subTitle;
        missonModel.isFromChatPage = YES;
        
        RootWebViewController *webView = [[RootWebViewController alloc] initWithUrl:file.absoluteString];
        webView.isShowCloseBtn = YES;
        webView.title = @"资讯详请";
        webView.showPageTitle = NO;
        webView.isShowShare = YES;
        webView.model = missonModel;
        [self.navigationController pushViewController:webView animated:YES];
    }else if (message.type == HEIMMessageTypeInfoVideo) {
        HEIMInfoPageObject *infoPage = (HEIMInfoPageObject *)message.messageObject;

        HEEduVideoDetailsController *vc = [HEEduVideoDetailsController new];
        vc.infoId = infoPage.id;
        vc.isFromChatPage = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)onTapAvatar:(HEIMMessage *)message {
    if (!message.isOwn) {
        HEDoctorMainPageViewController *vc = [HEDoctorMainPageViewController new];
        vc.userId = message.from;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)onLongPressAvatar:(HEIMMessage *)message {
    if (!message.isOwn) {
        [self.chatBar addATPerson: [[HEIMKit sharedKit] infoByUser:message.from] isLong:YES];
    }
}

#pragma mark - UITableViewDelegate

#pragma mark - JPSuspensionEntranceProtocol

- (BOOL)jp_isHideNavigationBar {
    return YES;
}

- (NSString *)jp_suspensionCacheMsg {
    return @"";
}

- (UIImage *)jp_suspensionLogoImage {
    return IMAGE_NAMED(@"group_select");
}

- (void)jp_requestSuspensionLogoImageWithLogoView:(UIImageView *)logoView {

    __weak typeof(self) weakSelf = self;
    [logoView sd_setImageWithURL:[NSURL URLWithString: _receiveInfo.avatarUrlString] placeholderImage:[UIImage imageNamed:@"noImage"] options:SDWebImageTransformAnimatedImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (weakSelf) weakSelf.logoImage = image;
    }];
}

- (void)jp_suspensionDidRemove {
    [[HECircleChatManager sharedManager] leaveRoom];
}

#pragma mark - lazy load
#pragma mark - setter

- (void)setIsSuspension:(BOOL)isSuspension {
    _isSuspension = isSuspension;
}

- (void)setIsHideNavBar:(BOOL)isHideNavBar {
    _isHideNavBar = isHideNavBar;
}

- (HEIMForward *)imDataForward {
    if (!_imDataForward) {
        _imDataForward = [HEIMForward new];
    }
    return _imDataForward;
}

- (NSMutableArray<HEIMMessageModel *> *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight , KScreenWidth, KScreenHeight - ZXNavBarHeight - 49 - KSafeTabbarHeight) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollsToTop = YES;
        _tableView.tableHeaderView = [[UIView alloc] init];
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
            [UITableView appearance].sectionHeaderTopPadding = 0;
        } else {
            // Fallback on earlier versions
        }
        
        _tableView.backgroundColor = CViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (HEChatBar *)chatBar {
    if (!_chatBar) {
        _chatBar = [[HEChatBar alloc] initWithFrame:CGRectZero type: HEIMUserModelTypeRoom];
        _chatBar.delegate = self;
        _chatBar.session = [[HEIMConversationManager sharedManager] sessionById: _roomId];
        [self.view addSubview: _chatBar];
        [self.view bringSubviewToFront:_chatBar];
    }
    return _chatBar;
}

- (HERoomUserTopView *)userTopView {
    if (!_userTopView) {
        _userTopView = [[HERoomUserTopView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, kScreenWidth, 75)];
        _userTopView.superVC = self;
    }
    return _userTopView;
}

- (NSMutableArray<HEIMMessageModel *> *)cacheSendMsg {
    if (!_cacheSendMsg) {
        _cacheSendMsg = @[].mutableCopy;
    }
    return _cacheSendMsg;
}

@end
